#!/usr/bin/env python3
"""
测试运行脚本
运行所有测试文件
"""

import os
import sys
import subprocess
from pathlib import Path

def run_unit_tests():
    """运行单元测试"""
    print("=== 运行单元测试 ===")
    
    test_dir = Path(__file__).parent / "unit_tests"
    
    test_files = [
        "test_agent_system.py",
        "test_simple_agent.py",
        "test_intent_improvement.py",
        "test_intent_verification.py",
        "test_amap_navigation.py",
        "test_llm_config.py",
        "test_result_processor.py",
        "test_text_utils.py"
    ]
    
    for test_file in test_files:
        test_path = test_dir / test_file
        if test_path.exists():
            print(f"\n运行 {test_file}...")
            try:
                result = subprocess.run([sys.executable, str(test_path)], 
                                      capture_output=True, text=True, cwd=Path(__file__).parent.parent)
                if result.returncode == 0:
                    print(f"✓ {test_file} 通过")
                else:
                    print(f"✗ {test_file} 失败")
                    print(f"错误: {result.stderr}")
            except Exception as e:
                print(f"✗ {test_file} 运行出错: {e}")
        else:
            print(f"⚠ {test_file} 不存在")

def run_integration_tests():
    """运行集成测试"""
    print("\n=== 运行集成测试 ===")
    
    test_dir = Path(__file__).parent / "integration_tests"
    
    test_files = [
        "test_agent_demo.py",
        "test_agent_usage.py",
        "test_retrieval_integration.py"
    ]
    
    for test_file in test_files:
        test_path = test_dir / test_file
        if test_path.exists():
            print(f"\n运行 {test_file}...")
            try:
                result = subprocess.run([sys.executable, str(test_path)], 
                                      capture_output=True, text=True, cwd=Path(__file__).parent.parent)
                if result.returncode == 0:
                    print(f"✓ {test_file} 通过")
                else:
                    print(f"✗ {test_file} 失败")
                    print(f"错误: {result.stderr}")
            except Exception as e:
                print(f"✗ {test_file} 运行出错: {e}")
        else:
            print(f"⚠ {test_file} 不存在")

def run_pytest():
    """使用pytest运行测试"""
    print("\n=== 使用pytest运行测试 ===")
    
    try:
        result = subprocess.run([sys.executable, "-m", "pytest", ".", "-v"], 
                              capture_output=True, text=True, cwd=Path(__file__).parent)
        print(result.stdout)
        if result.stderr:
            print("错误输出:", result.stderr)
    except Exception as e:
        print(f"pytest运行出错: {e}")

def main():
    """主函数"""
    print("旅游助手Agent系统测试运行器")
    print("=" * 50)
    
    # 检查当前目录
    current_dir = Path(__file__).parent
    print(f"当前目录: {current_dir}")
    
    # 运行单元测试
    run_unit_tests()
    
    # 运行集成测试
    run_integration_tests()
    
    # 使用pytest运行所有测试
    run_pytest()
    
    print("\n" + "=" * 50)
    print("测试运行完成！")

if __name__ == "__main__":
    main()
